import { useReducer } from 'react';

/**
 * 定义初始状态信息
 */
export interface IstateType {
  selectAssetList: Record<string, any>[];
  addAssetList: Record<string, any>[];
}

/**
 * 新增和编辑页面操作枚举
 * @SELECT_ASSET  存入选择资产数据
 *
 */
export enum IAssetListActionEnum {
  SELECT_ASSET = 'selectAsset',
}

/**
 * 定义新增和编辑页面操作匹配
 * @type 操作类型
 * @payload 传递数据
 */
export interface IActionType {
  type: IAssetListActionEnum;
  payload?: any;
}

export const useAssetListManageReducer = () => {
  /** 初始化状态 */
  const initState: IstateType = {
    selectAssetList: [],
    addAssetList: [],
  };

  const assetListManageReducer = (state: IstateType, action: IActionType) => {
    switch (action?.type) {
      /** 选中的合同列表 */
      case IAssetListActionEnum.SELECT_ASSET:
        return {
          ...state,
          selectAssetList: action?.payload,
        };

      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, assetListDispatch] = useReducer(assetListManageReducer, initState);

  return {
    ...state,
    assetListDispatch,
  };
};
